# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2023/4/24 11:26
# @Author : xyjiang1216
# @Email : jiangxuyan@nudt.edu.cn
# @File : layer_5_planning_traffic.py
# @Version: OpenPlanner2.0
import importlib

from OpenPlanner.core.api.solver_interface import PlanningStrategy
from OpenPlanner.core.src.op_class import OPClass


class TrafficPlanningModule:
    def __init__(self,
                 op_obj: OPClass):
        self.op_obj = op_obj

        self.planning_strategy: PlanningStrategy = None

    def solving(self):
        # 动态加载自定义算法类
        class_name = self.op_obj.algo_strategy
        base_module = self.op_obj.algo_base_module
        try:
            module = importlib.import_module(
                base_module
            )
        except ModuleNotFoundError:
            print("ModuleNotFoundError:"
                  "算法模块%s不存在，"
                  "请检查路径后更新配置文件" % base_module)
            exit(0)
        try:
            PlanningStrategy = getattr(module, class_name)
        except AttributeError:
            print("AttributeError:"
                  "算法模块%s中不存在"
                  "规划策略%s，"
                  "请检查路径后更新配置文件"
                  % (base_module, class_name))
            exit(0)

        print('The planning strategy is %s' % class_name)
        self.planning_strategy = PlanningStrategy(
            self.op_obj.algo_input.link_array,
            self.op_obj.algo_input.stream_array,
            self.op_obj.algo_input.instance_array,
            self.op_obj.solution_filepath,
            **self.op_obj.algo_param
        )

        self.planning_strategy.do_planning()
